package mo.leetcode_hot;

import java.util.ArrayList;
import java.util.List;

public class L39 {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        dfs(res, combination, candidates, target, 0);
        return res;
    }


    private void dfs(List<List<Integer>> res, List<Integer> combination, int[] candidates, int target, int idx) {
        if (idx == candidates.length) {
            return;
        }
        if (target == 0) {
            res.add(new ArrayList<>(combination));
            return;
        }
        dfs(res, combination, candidates, target, idx+1);
        if (target - candidates[idx] >= 0) {
            combination.add(candidates[idx]);
            dfs(res, combination, candidates, target-candidates[idx], idx);
            combination.remove(combination.size()-1);
        }
    }

}
